package com.bye.xiaomi_mall.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bye.xiaomi_mall.mapper.TdCouponMapper;
import com.bye.xiaomi_mall.domain.TdCoupon;
import com.bye.xiaomi_mall.service.ITdCouponService;

/**
 * 优惠券Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-15
 */
@Service
public class TdCouponServiceImpl implements ITdCouponService {
    @Autowired
    private TdCouponMapper tdCouponMapper;

    /**
     * 查询优惠券
     *
     * @param id 优惠券主键
     * @return 优惠券
     */
    @Override
    public TdCoupon selectTdCouponById(Long id) {
        return tdCouponMapper.selectTdCouponById(id);
    }

    /**
     * 查询优惠券列表
     *
     * @param tdCoupon 优惠券
     * @return 优惠券
     */
    @Override
    public List<TdCoupon> selectTdCouponList(TdCoupon tdCoupon) {
        List<TdCoupon> tdCoupons = tdCouponMapper.selectTdCouponList(tdCoupon);
        for (TdCoupon coupon : tdCoupons) {
            LocalDateTime failureTime = coupon.getFailureTime();
            LocalDateTime now = LocalDateTime.now();
            if (failureTime.isBefore(now)) {
                if ("1".equals(coupon.getStatus())){
                    coupon.setStatus("0");
                    tdCouponMapper.updateTdCoupon(coupon);
                }
//                System.out.println("优惠券过期了");
            }
        }

        return tdCoupons;
    }

    /**
     * 新增优惠券
     *
     * @param tdCoupon 优惠券
     * @return 结果
     */
    @Override
    public int insertTdCoupon(TdCoupon tdCoupon) {
        return tdCouponMapper.insertTdCoupon(tdCoupon);
    }

    /**
     * 修改优惠券
     *
     * @param tdCoupon 优惠券
     * @return 结果
     */
    @Override
    public int updateTdCoupon(TdCoupon tdCoupon) {
        return tdCouponMapper.updateTdCoupon(tdCoupon);
    }

    /**
     * 批量删除优惠券
     *
     * @param ids 需要删除的优惠券主键
     * @return 结果
     */
    @Override
    public int deleteTdCouponByIds(Long[] ids) {
        return tdCouponMapper.deleteTdCouponByIds(ids);
    }

    /**
     * 删除优惠券信息
     *
     * @param id 优惠券主键
     * @return 结果
     */
    @Override
    public int deleteTdCouponById(Long id) {
        return tdCouponMapper.deleteTdCouponById(id);
    }
}
